package com.exam.serviceimpl;

import com.exam.entity.PaperManage;
import com.exam.entity.examlx;
import com.exam.mapper.PaperMapper;
import com.exam.service.PaperService;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;


import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Service
public class PaperServiceImpl implements PaperService {

    @Autowired
    private PaperMapper paperMapper;
    @Override
    public List<PaperManage> findAll() {
        return paperMapper.findAll();
    }
    @Override
    public  List<examlx> findAlllx(String examlx) {
        System.out.printf("examlx111==="+examlx);
        List<examlx> bugList = new ArrayList<>();
        String tablename="";
        if(examlx.equals("选择题")){
            tablename="multi_question";
        }else if(examlx.equals("判断题")){
            tablename="judge_question";
        }else{
            tablename="fill_question";
        }
        bugList= paperMapper.download(tablename);
        System.out.printf("bugList==="+bugList);
        return bugList;
    }
    @Override
    public List<PaperManage> findById(Integer paperId) {
        return paperMapper.findById(paperId);
    }

    @Override
    public int add(PaperManage paperManage) {
        return paperMapper.add(paperManage);
    }
    @Override
    public void download(HttpServletResponse response,String examlx) {

            List<examlx> bugList = null;

            // 创建工作簿
            HSSFWorkbook workbook = new HSSFWorkbook();

            // 表头
            String lx=examlx.split(":")[1].toString().replace("\"","").replace("}","");
              System.out.printf("examlx===="+lx);
                String tablename="";
                if(lx.equals("选择题")){
                    tablename="multi_question";
                }else if(lx.equals("判断题")){
                    tablename="judge_question";
                }else{
                    tablename="fill_question";
                }
        bugList= paperMapper.download(tablename);
        String fileName = lx + "模板.xls";
        // 创建表
        HSSFSheet sheet = workbook.createSheet(lx+"试题模板");
        // 创建单元格样式
//        HSSFCellStyle cellStyle = workbook.createCellStyle();
        //获取样式对象
        sheet.setDefaultRowHeight((short) (4 * 256));
        CellStyle cellStyle = workbook.createCellStyle();

//设置样式对象，这里仅设置了边框属性

        cellStyle.setBorderBottom(BorderStyle.THIN); //下边框
        cellStyle.setBorderLeft(BorderStyle.THIN);//左边框
        cellStyle.setBorderTop(BorderStyle.THIN);//上边框
        cellStyle.setBorderRight(BorderStyle.THIN);//右边框
        cellStyle.setWrapText(true);//设置自动换行
        cellStyle.setAlignment(HorizontalAlignment.CENTER); //居中
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); //垂直
        String[] headers =new String[bugList.size()];
        for(int i=0;i<bugList.size();i++){
            headers[i]= String.valueOf(bugList.get(i).getMc());
        }
        /*String[] headers = {"企业编号","企业名称","统一社会信用代码","类型",
                "法定代表人","法定代表人手机号码","法定代表人证件号码","传真号码",
                "座机号码","办公号码","邮箱地址","营业执照号","注册资本","单位性质",
                "省","市","县区","详细地址","联系人","联系人电话","企业网址","经营范围"};*/
        HSSFRow row = sheet.createRow(0);
        for(int i=0;i<headers.length;i++){
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
//                POI中背景颜色设置方法如下:
            cellStyle.setFillBackgroundColor(HSSFColor.SKY_BLUE.index);
//            HSSFCellStyle.BORDER_THIN POI 3.17后版本设置边框 BorderStyle.THIN
/*            cellStyle.setBorderBottom(BorderStyle.THIN); //下边框
            cellStyle.setBorderLeft(BorderStyle.THIN);//左边框
            cellStyle.setBorderTop(BorderStyle.THIN);//上边框
            cellStyle.setBorderRight(BorderStyle.THIN);//右边框*/
            HSSFFont font = workbook.createFont();
            font.setBold(true);
            font.setColor(HSSFColor.SKY_BLUE.index);
            font.setFontHeightInPoints((short) 20);
            cellStyle.setFont(font);
            sheet.setColumnWidth(i,6000);
            cell.setCellStyle(cellStyle);
            if(text.toString().contains("内容")){
//                内容列变长
                sheet.setColumnWidth(i,13000);
            }
            if(text.toString().contains("题目")){
//                内容列变长
                sheet.setColumnWidth(i,13000);
            }
            cell.setCellStyle(cellStyle);
        }
        response.setContentType("text/html;charset=UTF-8");
//        response.setContentType("application/octet-stream");
        response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
//        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.setCharacterEncoding("UTF-8");//解决导出文件名称中文乱码
        try {
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setCharacterEncoding("UTF-8");
        try {
            response.flushBuffer();
            OutputStream outputStream = response.getOutputStream();
            workbook.write(response.getOutputStream());
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }



///*    @Override
   /* public void exportDataToEx(HttpServletResponse response) throws IOException {


        // 新增数据行，并且设置单元格数据
        int rowNum = 1;
        // headers表示excel表中第一行的表头 在excel表中添加表头
        String[] headers = {"企业编号","企业名称","统一社会信用代码","类型",
                "法定代表人","法定代表人手机号码","法定代表人证件号码","传真号码",
                "座机号码","办公号码","邮箱地址","营业执照号","注册资本","单位性质",
                "省","市","县区","详细地址","联系人","联系人电话","企业网址","经营范围"};
        HSSFRow row = sheet.createRow(0);
        for(int i=0;i<headers.length;i++){
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }
        //在表中存放查询到的数据放入对应的列
        for (TConstructorVo tConstructor : allTConstructor) {
            HSSFRow row1 = sheet.createRow(rowNum);
            row1.createCell(0).setCellValue(tConstructor.getNoid());
            row1.createCell(1).setCellValue(tConstructor.getConstructorName());
            row1.createCell(2).setCellValue(tConstructor.getConstructorCode());
            row1.createCell(3).setCellValue(tConstructor.getTypeName());
            row1.createCell(4).setCellValue(tConstructor.getFarenDaibiao());
            row1.createCell(5).setCellValue(tConstructor.getFarenDaibiaoPhone());
            row1.createCell(6).setCellValue(tConstructor.getFarenIdcare());
            row1.createCell(7).setCellValue(tConstructor.getChuanzhenNum());
            row1.createCell(8).setCellValue(tConstructor.getZuojiNum());
            row1.createCell(9).setCellValue(tConstructor.getWorkPhone());
            row1.createCell(10).setCellValue(tConstructor.getConstructorEmail());
            row1.createCell(11).setCellValue(tConstructor.getYingyezhizhaohao());
            row1.createCell(12).setCellValue(tConstructor.getRegisteredCapital());
            row1.createCell(13).setCellValue(tConstructor.getNName());
            row1.createCell(14).setCellValue(tConstructor.getPname());
            row1.createCell(15).setCellValue(tConstructor.getCname());
            row1.createCell(16).setCellValue(tConstructor.getCountyname());
            row1.createCell(17).setCellValue(tConstructor.getAddress());
            row1.createCell(18).setCellValue(tConstructor.getLianxiren());
            row1.createCell(19).setCellValue(tConstructor.getTelphone());
            row1.createCell(20).setCellValue(tConstructor.getQiyewangzhi());
            row1.createCell(21).setCellValue(tConstructor.getJingyingfanwei());
            rowNum++;
        }

        response.setContentType("application/octet-stream");
        response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        OutputStream outputStream = response.getOutputStream();
        workbook.write(response.getOutputStream());
        outputStream.flush();
        outputStream.close();
    }*/

}
